package com.itheima.api.returnreason;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.returnreason.OmsOrderReturnReasonMapper;
import com.itheima.vo.pojo.OmsOrderReturnReason;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class OmsOrderReturnReasonServiceImpl
        extends ServiceImpl<OmsOrderReturnReasonMapper, OmsOrderReturnReason>
        implements OmsOrderReturnReasonService {

    @DubboReference
    private OmsOrderReturnReasonService reasonService;

    @Autowired
    private OmsOrderReturnReasonMapper reasonMapper;

    /**
     * 分页查询退货原因
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Map limit(Integer pageNum, Integer pageSize) {
        //创建分页构造器
        Page<OmsOrderReturnReason> page = new Page<>(pageNum, pageSize);
        reasonService.page(page);
        //设置返回值
        Map map = new HashMap();
        map.put("list", page.getRecords());
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put(" total", page.getTotal());
        map.put(" totalPage", page.getPages());

        return map;
    }

    /**
     * 批量删除退货原因
     *
     * @param ids
     */
    public void deleteByIds(List<Integer> ids) {
        if (ids != null) {
            reasonService.removeByIds(ids);
        }
    }

    /**
     * 修改退货原因启用状态
     * @param status
     * @param ids
     */
    public void updateStatus(Integer status,List<Integer> ids) {
        LambdaUpdateWrapper<OmsOrderReturnReason> lqw = new LambdaUpdateWrapper<>();
        lqw.in(ids != null, OmsOrderReturnReason::getId, ids);
        lqw.set(status != null, OmsOrderReturnReason::getStatus, status);
        reasonService.update(lqw);
    }

    /**
     * 修改退货原因
     *
     * @param id
     * @param reason
     */
    public void updateReason(Integer id, OmsOrderReturnReason reason) {
        //创建条件构造器
        LambdaUpdateWrapper<OmsOrderReturnReason> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(id != null, OmsOrderReturnReason::getId, id);
        reasonService.update(reason,lqw);
    }


}
